package com.taotao.web.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.taotao.entity.*;
import com.taotao.entity.item.category.ResultCategory;
import com.taotao.entity.item.item.AddItem;
import com.taotao.service.ItemService;
import com.taotao.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/item")
public class ItemController {
    @Resource
    private ItemService itemService;

    // 商品所有
    @ResponseBody
    @RequestMapping("/list")
    public PageInff findAll(Integer page, Integer rows) {
        PageInff pageInff = new PageInff();
        Page<Object> p = PageHelper.startPage(page, rows);
        itemService.findAll();
        pageInff.setTotal(p.getTotal())
                .setRows(p.getResult());
        return pageInff;
    }

    // 规格所有
    @ResponseBody
    @RequestMapping("/param/list")
    public PageInff findItemParamAll(Integer page, Integer rows) {
        PageInff pageInff = new PageInff();
        Page<Object> p = PageHelper.startPage(page, rows);
        itemService.findItemParamAll();
        pageInff.setTotal(p.getTotal())
                .setRows(p.getResult());
        return pageInff;
    }

    // 新增商品
    @RequestMapping("/save")
    @ResponseBody
    public ResultVO addItem(AddItem addItem) throws IOException {
        Item item = new Item();
        ItemDesc itemDesc = new ItemDesc();
        ItemParamItem itemParamItem = new ItemParamItem();
        // 添加 item
        item.setCid(addItem.getCid());
        item.setBarcode(addItem.getBarcode());
        item.setCreated(new Date());
        item.setUpdated(new Date());
        item.setTitle(addItem.getTitle());
        item.setImage(addItem.getImage());
        item.setNum(addItem.getNum());
        item.setPrice(addItem.getPrice());
        item.setStatus((byte) 1);
        item.setSellPoint(addItem.getSellPoint());
        long itemId = itemService.addItem(item);
        // 添加详情
        itemDesc.setCreated(new Date());
        itemDesc.setItemDesc(addItem.getDesc());
        itemDesc.setItemId(itemId);
        itemDesc.setUpdated(new Date());
        itemService.addItemDesc(itemDesc);
        // 添加param
        itemParamItem.setItemId(addItem.getCid());
        itemParamItem.setCreated(new Date());
        itemParamItem.setUpdated(new Date());
        itemParamItem.setParamData(addItem.getItemParams());
        itemService.addItemParamItem(itemParamItem);
        ResultVO resultVO = new ResultVO();
        resultVO.setStatus(200);
        resultVO.setMessage("ok");
        return resultVO;
    }



    // 修改商品规格参数
    @ResponseBody
    @RequestMapping("/updateItemParam")
    public Object updateItemParam(Long itemCatId, String paramData) {
        // todo 修改返回值
        return itemService.updateItemParam(itemCatId, paramData);
    }

    // 删除商品规格参数
    @ResponseBody
    @RequestMapping("/param/delete")
    public ResultVO delItemParam(@RequestParam("ids") Long[] ids) {
        int param = itemService.delItemParam(ids);
        ResultVO resultVO = new ResultVO();
        if (param > 0) {
            resultVO.setStatus(200);
        }
        return resultVO;
    }

    // 新加规格参数
    @ResponseBody
    @RequestMapping("/param/save/{id}")
    public ResultCategory addItemParam(@PathVariable long id, String paramData) {
        itemService.addItemParam(id, paramData);
        ResultCategory resultCategory = new ResultCategory();
        resultCategory.setStatus(200);
        resultCategory.setMsg("ok");
        return resultCategory;
    }

    @RequestMapping("/param/query/itemcatid/{parentId}")
    @ResponseBody
    public ResultCategory getItemCat(@PathVariable long parentId) {
        ItemParam itemParam = itemService.findCidByItemParam(parentId);
        ResultCategory resultCategory = new ResultCategory();
        resultCategory.setStatus(200);
        resultCategory.setMsg("ok");
        resultCategory.setData(itemParam);
        return resultCategory;
    }
}
